今天我們來實作判斷棋子與指定格之間是否被阻擋的方法,以及判斷是否被將軍的方法,我們可以將它們寫在ChessmanRulesData當中
大致上分為3個部分,判斷直行、橫行、斜行,其中有無被棋子阻擋
// 判斷是否允許移動棋子
public boolean isEnableMove(int start,int stop,HashMap<String,Object> chessboardData){
int startCoordinate=0,stopCoordinate=0,startCoordinate2=0,stopCoordinate2=0;
Boolean enableMove=false;
if(String.valueOf(playerChessboardData.getStringBoardData(start).charAt(0))
.equals(String.valueOf(playerChessboardData.getStringBoardData(stop).charAt(0)))){
// 判斷直行
for(int i=0;i<coordinate_n.length;i++){
if(String.valueOf(playerChessboardData.getStringBoardData(start).charAt(1)).equals(coordinate_n[i])){
startCoordinate=i;
}
if(String.valueOf(playerChessboardData.getStringBoardData(stop).charAt(1)).equals(coordinate_n[i])){
stopCoordinate=i;
}
}
if(Math.min(startCoordinate,stopCoordinate)+1 == Math.max(startCoordinate,stopCoordinate)){
enableMove = true;
}else{
for(int i=Math.min(startCoordinate,stopCoordinate)+1;i<Math.max(startCoordinate,stopCoordinate);i++){
// Log.d("TAG", "Move: "+chessboardData.get(String.valueOf(playerChessboardData.getStringBoardData(start).charAt(0)+coordinate_n[i])));
if(!(chessboardData.get(String.valueOf(playerChessboardData.getStringBoardData(start).charAt(0)+coordinate_n[i]))
.equals(" "))){
// Log.d("TAG", "Move: 直行中間有棋子阻擋");
enableMove=false;
break;
}else{
enableMove=true;
}
}
}
} else if (String.valueOf(playerChessboardData.getStringBoardData(start).charAt(1))
.equals(String.valueOf(playerChessboardData.getStringBoardData(stop).charAt(1)))) {
// 判斷橫行
for(int i=0;i<coordinate_a.length;i++){
if(String.valueOf(playerChessboardData.getStringBoardData(start).charAt(0)).equals(coordinate_a[i])){
startCoordinate=i;
}
if(String.valueOf(playerChessboardData.getStringBoardData(stop).charAt(0)).equals(coordinate_a[i])){
stopCoordinate=i;
}
}
if(Math.min(startCoordinate,stopCoordinate)+1 == Math.max(startCoordinate,stopCoordinate)){
enableMove = true;
}else{
for(int i=Math.min(startCoordinate,stopCoordinate)+1;i<Math.max(startCoordinate,stopCoordinate);i++){
// Log.d("TAG", "Move: "+chessboardData.get(coordinate_a[i]+(String.valueOf(playerChessboardData.getStringBoardData(start).charAt(1)))));
if(!(chessboardData.get(coordinate_a[i]+(String.valueOf(playerChessboardData.getStringBoardData(start).charAt(1))))
.equals(" "))){
// Log.d("TAG", "Move: 橫行中間有棋子阻擋");
enableMove=false;
break;
}else{
enableMove=true;
}
}
}
} else {
// 判斷斜行
ArrayList arr1 = new ArrayList<>();
ArrayList arr2 = new ArrayList<>();
// 如果是移動騎士則無視阻擋
if(chessboardData.get(String.valueOf(playerChessboardData.getStringBoardData(start))).equals("wN")
|| chessboardData.get(String.valueOf(playerChessboardData.getStringBoardData(start))).equals("bN")){
enableMove = true;
}else{
for(int i=0;i<coordinate_a.length;i++){
if(String.valueOf(playerChessboardData.getStringBoardData(start).charAt(0)).equals(coordinate_a[i])){
startCoordinate=i;
}
if(String.valueOf(playerChessboardData.getStringBoardData(stop).charAt(0)).equals(coordinate_a[i])){
stopCoordinate=i;
}
if(String.valueOf(playerChessboardData.getStringBoardData(start).charAt(1)).equals(coordinate_n[i])){
startCoordinate2=i;
}
if(String.valueOf(playerChessboardData.getStringBoardData(stop).charAt(1)).equals(coordinate_n[i])){
stopCoordinate2=i;
}
}
int size = Math.abs(startCoordinate - stopCoordinate);
for(int i=1;i<size;i++){
if(startCoordinate > stopCoordinate){
arr1.add(startCoordinate-1);
startCoordinate = startCoordinate-1;
}else if(startCoordinate < stopCoordinate){
arr1.add(startCoordinate+1);
startCoordinate = startCoordinate+1;
}
if(startCoordinate2 > stopCoordinate2){
arr2.add(startCoordinate2-1);
startCoordinate2 = startCoordinate2-1;
}else if(startCoordinate2 < stopCoordinate2){
arr2.add(startCoordinate2+1);
startCoordinate2 = startCoordinate2+1;
}
}
if(size == 1){
enableMove = true;
}
for(int i=0;i<arr1.size();i++){
// Log.d("20240403", ""+chessboardData.get(String.valueOf(coordinate_a[(int)arr1.get(i)]+coordinate_n[(int)arr2.get(i)])));
if(!(chessboardData.get(coordinate_a[(int)arr1.get(i)]+coordinate_n[(int)arr2.get(i)])
.equals(" "))){
// Log.d("TAG", "Move: 斜線中間有棋子阻擋");
enableMove=false;
break;
}else{
enableMove=true;
}
}
}
}
return enableMove;
}
檢查某格是否被將軍(被其他棋子瞄準),這裡使用較為原始的作法逐一判斷
public boolean isCheck(int position,HashMap<String,Object> chessboardData){
Boolean check=false;
int up = position/8;
int left = position%8;
int down = 7-position/8;
int right = 7-position%8;
if(up>2){
if(left>1){
if(chessboardData.get(playerChessboardData.getStringBoardData(position-17)).equals(other+"N")){
check = true;
}
}
if(right>1){
if(chessboardData.get(playerChessboardData.getStringBoardData(position-15)).equals(other+"N")){
check = true;
}
}
}
if(down>2){
if(left>1){
if(chessboardData.get(playerChessboardData.getStringBoardData(position+15)).equals(other+"N")){
check = true;
}
}
if(right>1){
if(chessboardData.get(playerChessboardData.getStringBoardData(position+17)).equals(other+"N")){
check = true;
}
}
}
if(left>2){
if(up>1){
if(chessboardData.get(playerChessboardData.getStringBoardData(position-10)).equals(other+"N")){
check = true;
}
}
if(down>1){
if(chessboardData.get(playerChessboardData.getStringBoardData(position+6)).equals(other+"N")){
check = true;
}
}
}
if(right>2){
if(up>1){
if(chessboardData.get(playerChessboardData.getStringBoardData(position-6)).equals(other+"N")){
check = true;
}
}
if(down>1){
if(chessboardData.get(playerChessboardData.getStringBoardData(position+10)).equals(other+"N")){
check = true;
}
}
}
for(int i=0;i<8;i++){
if(position!=left+i*8) {
if(chessboardData.get(playerChessboardData.getStringBoardData(left+i*8)).equals(other+"R")
&& isEnableMove(left+i*8,position,chessboardData)){
check = true;
break;
}
}
if(position!=up*8+i) {
if(chessboardData.get(playerChessboardData.getStringBoardData(up*8+i)).equals(other+"R")
&& isEnableMove(up*8+i,position,chessboardData)){
check = true;
break;
}
}
}
for(int i=0;i<64;i++){
if(i != position){
for(int j=1;j<=Math.min(up,left);j++){
if(i == position - 9*j){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"B")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
for(int j=1;j<=Math.min(down,left);j++){
if(i == position + 7*j){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"B")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
for(int j=1;j<=Math.min(down,right);j++){
if(i == position + 9*j){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"B")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
for(int j=1;j<=Math.min(up,right);j++){
if(i == position - 7*j){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"B")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
}
}
for(int i=0;i<64;i++){
if(i != position){
for(int j=1;j<=Math.min(up,left);j++){
if(i == position - 9*j){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"Q")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
for(int j=1;j<=Math.min(down,left);j++){
if(i == position + 7*j){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"Q")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
for(int j=1;j<=Math.min(down,right);j++){
if(i == position + 9*j){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"Q")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
for(int j=1;j<=Math.min(up,right);j++){
if(i == position - 7*j){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"Q")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
if((i % 8 == position % 8) || (i / 8 == position / 8)){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"Q")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
}
for(int i=0;i<64;i++){
if(i != position){
if(up>0 && left>0){
if(i == position - 9){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"K")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
if(down>0 && left>0){
if(i == position + 7){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"K")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
if(down>0 && right>0){
if(i == position + 9){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"K")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
if(up>0 && right>0){
if(i == position - 7){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"K")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
if(up>0){
if(i == position - 8){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"K")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
if(left>0){
if(i == position - 1){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"K")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
if(down>0){
if(i == position + 8){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"K")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
if(right>0){
if(i == position + 1){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(other+"K")
&& isEnableMove(i,position,chessboardData)){
check = true;
break;
}
}
}
}
}
if(up > 0){
if(chessboardData.get(playerChessboardData.getStringBoardData(position-7)).equals(other+"P")
|| chessboardData.get(playerChessboardData.getStringBoardData(position-9)).equals(other+"P")){
check = true;
}
}
return check;
}
這樣我們就將棋子走法部分的data建置完成了